﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using PopupControl;
using System.Dynamic;
//using Newtonsoft.Json.Linq;

namespace _180FrameWork.CustControls
{
    public partial class CustFuzzyQueryList : UserControl
    {
        public event EventHandler ItemSelected;

        public CustFuzzyQueryList()
        {
            InitializeComponent();
            DoubleBuffered = true;
            //ResizeRedraw = true;
        }

        public object DataSource
        {
            get { return this.bs.DataSource; }
            set
            {
                this.bs.DataSource = value; 
                if (this.dgDict.DataSource == null)
                {
                    this.dgDict.DataSource = bs;
                }
            }
        }

        public object GetSelectedItem()
        {
            if (this.bs.Current != null)
            {
                return this.bs.Current ;
            }
            else
                return null;
        }

        public string GetSelectedItemText(string Key)
        {
            if (this.bs.Current != null)
            {
                foreach (DataGridViewColumn col in this.dgDict.Columns)
                {
                    if (col.DataPropertyName == Key)
                    {
                        var cell = this.dgDict[col.Name, this.bs.Position];
                        return cell.Value.ToString();
                    }
                }
                return "";
            }
            else
                return "";
        }

        public void SelectPreviousItem() {
            bs.MovePrevious();
        }

        public void SelectNextItem()
        {
            bs.MoveNext();
        }

        //protected override void WndProc(ref Message m)
        //{
        //    if (Parent != null)
        //    {
        //        if ((Parent as Popup).ProcessResizing(ref m))
        //        {
        //            return;
        //        }
        //        base.WndProc(ref m);
        //    }
        //}

        private void dgDict_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dgDict_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.ItemSelected != null)
                this.ItemSelected(this, new EventArgs());
        }
    }
}
